package com.cg.service.impl;

import com.cg.dao.NoticeDepartmentMapper;
import com.cg.dao.NoticeMapper;
import com.cg.dao.UserMapper;
import com.cg.dto.NoticeDepartmentDto;
import com.cg.dto.NoticePageDto;
import com.cg.entity.Notice;
import com.cg.entity.User;
import com.cg.service.NoticeService;
import com.cg.vo.NoticeVo;
import com.cg.vo.QueryNoticeByNoticeIdVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * (Notice)表服务实现类
 *
 * @author chengong
 * @since 2023-12-16 09:17:04
 */
@Service("noticeService")
public class NoticeServiceImpl implements NoticeService {
    @Resource
    private NoticeMapper noticeMapper;

    @Resource
    private NoticeDepartmentMapper noticeDepartmentMapper;


    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Resource
    private UserMapper userMapper;




    @Value("${topicName}")
    private String topicName;


    @Override
    public PageInfo<NoticeVo> noticeList(NoticePageDto noticePageDto) {
        PageHelper.startPage(noticePageDto.getPageNum(),noticePageDto.getPageSize());
        List<NoticeVo> noticeVoList = noticeMapper.noticeList(noticePageDto);
        PageInfo pageInfo = new PageInfo(noticeVoList);
        return pageInfo;
    }

    @Override
    @Transactional
    public Integer createNotice(NoticeDepartmentDto noticeDepartmentDto) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeDepartmentDto,notice);

        //生成UUID
        String noticeCode = UUID.randomUUID().toString().replaceAll("-","");
        notice.setNoticeCode(noticeCode);
        Integer count = noticeMapper.createNotice(notice);
        if(count>0){//公告添加成功
            if(notice.getNoticeType().equals(1)){
                //是全体公告
                return count;
            }
            noticeDepartmentDto.setNoticeId(notice.getId());
            count = noticeDepartmentMapper.createNoticeDepartment(noticeDepartmentDto);
            if(notice.getNoticeType().equals(2)){
                //是部门公告
                return count;
            }
            //实时公告

            //需要发送消息-发送消息的时候直接将信息发送到rocketMQ，让rocketMQ来发送
            List<User> userList = userMapper.queryUserListByDepId(noticeDepartmentDto.getDepartmentIdList());

            noticeDepartmentDto.setUserList(userList);

            rocketMQTemplate.syncSend(topicName,noticeDepartmentDto);
            return count;
        }
        return count;
    }

    /**
     * 根据部门id查询部门详细信息
     */
    @Override
    public QueryNoticeByNoticeIdVo queryNotice(Integer id) {
        return noticeMapper.queryNotice(id);
    }

    @Override
    public Integer deleteNoticeById(Integer noticeId) {
        Integer count = 0;
        //表里面没有数据
        //删除
         count = noticeMapper.deleteNoticeById(noticeId);
        return count;
    }

    @Override
    public Integer setIsShow() {
        return noticeMapper.setIsShow();
    }

    @Override
    public Integer setNotice(NoticeDepartmentDto noticeDepartmentDto) {

        Integer noticeId = noticeDepartmentDto.getNoticeId();
        QueryNoticeByNoticeIdVo queryNoticeByNoticeIdVo = noticeMapper.queryNotice(noticeId);
        BeanUtils.copyProperties(queryNoticeByNoticeIdVo,noticeDepartmentDto);



        //获取类型
        Integer noticeType = noticeDepartmentDto.getNoticeType();
        //删除之前的
        //noticeMapper.deleteNoticeById(noticeDepartmentDto.getNoticeId());
        noticeDepartmentMapper.removeNoticeDepartment(noticeDepartmentDto.getNoticeId());
        //然后添加数据库
        Integer count = noticeMapper.setNotice(noticeDepartmentDto);
        if(noticeType.equals(1)){
            //全局公告
            //删除之前的
            //然后添加数据库
            return count;
        }

        if(noticeType.equals(2)){
            //全局公告
            //删除之前的
            //删除部门，公告关联表
            //然后添加数据库
            //还要添加部门，公告关联表
            noticeDepartmentMapper.createNoticeDepartment(noticeDepartmentDto);
            return count;
        }

        if(noticeType.equals(3)){
            //全局公告
            //删除之前的
            //删除部门，公告关联表
            //然后添加数据库
            //还要添加部门，公告关联表
            noticeDepartmentMapper.createNoticeDepartment(noticeDepartmentDto);
            //还要发邮件

            List<User> userList = userMapper.queryUserListByDepId(noticeDepartmentDto.getDepartmentIdList());

            noticeDepartmentDto.setUserList(userList);

            System.out.println(noticeDepartmentDto.getUserList());
            rocketMQTemplate.syncSend(topicName,noticeDepartmentDto);
            return count;
        }

        return count;
    }
}
